# import subprocess
#
# # PlantUML时序图代码
# uml_code = """
# @startuml
# Alice -> Bob: 请求
# Bob --> Alice: 响应
# @enduml
# """
#
# # 保存到文件
# with open("sequence.puml", "w",encoding='utf-8') as f:
#     f.write(uml_code.strip())
#
# # 调用plantuml.jar生成图片（替换为你的plantuml.jar路径）
# subprocess.run(["java", "-jar", "m:/resources/plantuml.jar", "sequence.puml"])

from plantuml import PlantUML
import requests
from bs4 import BeautifulSoup

# PlantUML时序图代码
uml_code = """
@startuml
Alice -> Bob: 请求
Bob --> Alice: 响应
@enduml
"""

# 使用在线服务生成图片
plantuml_url = 'http://192.168.77.3:18080/plantuml/uml/'
response = requests.get(plantuml_url + uml_code)

def generate_online(uml_code, output_file="online_sequence.png"):
    # 使用PlantUML在线服务
    plantuml_url = 'http://192.168.77.3:18080/uml/png/'
    encoded_code = PlantUML(url=plantuml_url).get_url(uml_code)
    response = requests.get(plantuml_url + encoded_code)
    if response.status_code == 200:
        html_doc = str(response.content)
        soup = BeautifulSoup(html_doc, 'lxml')
        img_url = soup.find('input', id='url')['value']
        rs = requests.get('http://192.168.77.3:18080/'+img_url)
        
        with open(output_file, "wb") as f:
            f.write(rs.content)
        print(f"在线生成成功: {output_file}")
    else:
        print("生成失败，错误码:", response.status_code)

generate_online(uml_code=uml_code, output_file="online_sequence.png")